package cn.lb.permission.controller;


import cn.lb.annotation.LBPermission;
import cn.lb.permission.domain.Role;
import cn.lb.permission.dto.RoleMenuDTO;
import cn.lb.permission.dto.RolePermissionDTO;
import cn.lb.permission.query.RoleQuery;
import cn.lb.permission.service.IRoleService;
import cn.lb.basic.util.PageList;
import cn.lb.basic.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@LBPermission(name = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    /**
     * 增删改操作只需要告诉前端,操作成功还是失败就可以了
     * @param role
     * @return
     */
    // 新增/修改
    @LBPermission(name = "新增或修改")
    @PutMapping
    public R addOrUpdate(@RequestBody Role role){
        try {
            if(Objects.isNull(role.getId())){   // 如果ID为空,说明是新增操作
                roleService.insert(role);
            }else{
                // 修改操作
                roleService.update(role);
            }
            return R.me();
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("保存失败!"+e.getMessage());
        }
    }

    // 根据ID删除单条
    @LBPermission(name = "根据ID删除")
    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable("id")Long id){
        try {
            roleService.deleteById(id);
            return R.me();
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("删除失败!"+e.getMessage());
        }
    }

    // 批量删除
    @LBPermission(name = "批量删除")
    @PatchMapping
    public R batchDelete(@RequestBody List<Long> ids){
        try {
            roleService.deletebatch(ids);
            return R.me();
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("批量删除失败!"+e.getMessage());
        }
    }

    // 根据ID查询
    @LBPermission(name = "根据ID查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id")Long id){
        try {
            Role role = roleService.selectById(id);
            return R.me().setData(role);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("查询失败!"+e.getMessage());
        }
    }

    // 查询所有
    @LBPermission(name = "查询所有")
    @GetMapping
    public R getAll(){
        try {
            List<Role> roles = roleService.selectAll();
            return R.me().setData(roles);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("查询失败!"+e.getMessage());
        }
    }

    /**
     * 分页+高级查询
     * 入参: RoleQuery -> currentPage pageSize keyword
     * 返参: R -> success message dataObj:PageList(total,rows)
     */
    @LBPermission(name = "分页高级查询")
    @PostMapping
    public R listPage(@RequestBody RoleQuery query){
        try {
            PageList<Role> pageList = roleService.page(query);
            return R.me().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("分页查询失败!"+e.getMessage());
        }
    }

    @LBPermission(name = "设置权限")
    @PutMapping("/permission")
    public R saveRolePermission(@RequestBody RolePermissionDTO dto){
        try {
            roleService.saveRolePermission(dto);
            return R.me();
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("设置权限失败!"+e.getMessage());
        }
    }

    @LBPermission(name = "获取角色的权限")
    @GetMapping("/permissionsns/{roleId}")
    public R getRolePermissionSns(@PathVariable("roleId")Long roleId){
        try {
            List<String> sns = roleService.getRolePermissionSns(roleId);
            return R.me().setData(sns);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("获取角色的权限失败!"+e.getMessage());
        }
    }

    @LBPermission(name = "设置菜单")
    @PutMapping("/menu")
    public R saveRolePermission(@RequestBody RoleMenuDTO dto){
        try {
            System.out.println(dto.getMenu());
            System.out.println(dto.getRoleId());
            roleService.savemenus(dto);
            return R.me();
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("设置权限失败!"+e.getMessage());
        }
    }

    @LBPermission(name = "获取角色的菜单")
    @GetMapping("/menus/{roleId}")
    public R getRoleMenus(@PathVariable("roleId")Long roleId){
        try {
            List<Long> menus = roleService.getRoleMenus(roleId);
            return R.me().setData(menus);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("获取角色的权限失败!"+e.getMessage());
        }
    }
}
